package com.lc.hongtai.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import com.lc.hongtai.MyApplication;
import com.lc.hongtai.R;
import com.lc.hongtai.conn.JsonAccessToken;
import com.lc.hongtai.conn.JsonBorrowindex;
import com.lc.hongtai.toast.LovelyToast;
import com.lc.hongtai.view.BorrowPop;
import com.zcx.helper.activity.AppV4Fragment;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;


/**
 * A simple {@link Fragment} subclass.
 */
public class FourthFragment2 extends AppV4Fragment implements View.OnClickListener {


    private PopupWindow popLeft;
    private View layoutLeft;


    private ListView menulistLeft;
    private List<JsonBorrowindex.Info.ListContent> borrow_use = new ArrayList<>();
    private List<String> stringListBorrow_use = new ArrayList<>();

    private List<JsonBorrowindex.Info.ListContent> borrow_use_day = new ArrayList<>();


    private List<String> stringListBorrow_use_day = new ArrayList<>();


    private List<JsonBorrowindex.Info.ListContent> borrow_money = new ArrayList<>();
    private List<String> myborrow_money = new ArrayList<>();


    private List<JsonBorrowindex.Info.ListContent> sex = new ArrayList<>();
    private List<String> mysex = new ArrayList<>();

    private List<JsonBorrowindex.Info.ListContent> borrow_duration_month = new ArrayList<>();
    private List<String> myborrow_duration = new ArrayList<>();


    private List<JsonBorrowindex.Info.ListContent> retypement = new ArrayList<>();
    private List<String> str_retypement = new ArrayList<>();


    //还款方式id
    private String type_repyment;


    //借款期限id
    private String borrow_use_id;

    //奖励类型

    private List<JsonBorrowindex.Info.ListContent> reward_type = new ArrayList<>();

    //奖励类型id
    private String reward_type_id;
    private List<String> reward_type_list = new ArrayList<>();


//最高投资金额


    //最低投资金额
    private List<JsonBorrowindex.Info.ListContent> borrow_min = new ArrayList<>();

    private List<String> borrow_min_list = new ArrayList<>();
    //有效时间
    private List<JsonBorrowindex.Info.ListContent> borrow_time = new ArrayList<>();

    private List<String> borrow_time_list = new ArrayList<>();


    //最高投资
    private List<JsonBorrowindex.Info.ListContent> borrow_max = new ArrayList<>();
    private List<String> stringListBorrow_max = new ArrayList<>();

    //待收限制
    private List<JsonBorrowindex.Info.ListContent> daishou = new ArrayList<>();
    private List<String> daishou_str = new ArrayList<>();
    private String daishou_id = "0";

    List<String> data = new ArrayList<String>();


    //是否定向投标是
    @Bind(R.id.directional_sel_rb)
    CheckBox directional_sel_rb;


    //投标奖励
    @Bind(R.id.tender_award_sel_rb)
    CheckBox tender_award_sel_rb;


    //是否投标奖励
    @Bind(R.id.linearlayout_bili)
    LinearLayout linearlayout_bili;
    //投标比例
    @Bind(R.id.check_toubiaobili)
    RadioButton check_toubiaobili;
    //固定金额
    @Bind(R.id.check_gudingmoney)
    RadioButton check_gudingmoney;

    private String no_yeid;
    //定向投标密码
    @Bind(R.id.edit_pwd)
    EditText edit_pwd;

    //待收限制 不限制
    @Bind(R.id.nolimit)
    TextView nolimit;
    //全部待受
    @Bind(R.id.tender_award_nosel_rb)
    TextView tender_award_nosel_rb;
    //当天接受
    @Bind(R.id.daylimit)
    TextView daylimit;
    //全部接受
    @Bind(R.id.all_limit)
    TextView all_limit;

    private String antian_id = "no";


    private String dingxiang_id = "0";


    public List<JsonBorrowindex.Info.ListContent> borrow_type_list = new ArrayList<>();

    public List<String> list_borrow_type = new ArrayList<>();
    private String borrow_type_id;


    @Bind(R.id.toubiaojiangli)
    EditText toubiaojiangli;
    @Bind(R.id.editText)
    EditText editText;
    //待收money
    @Bind(R.id.edit_money)
    EditText edit_money;

    @Bind(R.id.img_first)
    ImageView img_first;
    @Bind(R.id.img_second)
    ImageView img_second;
    @Bind(R.id.img_third)
    ImageView img_third;
    @Bind(R.id.img_fourth)
    ImageView img_fourth;


    private boolean flag_jiangli = false;
    private BorrowPop borrowPop;

    public FourthFragment2() {
        // Required empty public constructor
    }

    public void getData() {
        for (int i = 0; i < 10; i++) {
            data.add("i" + i);
        }
    }

    private float popBottom;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment_fourth_beifen2, null)));
        ButterKnife.bind(this, view);

        initListener();

       /* et01.setText("");
        et03.setText("");*/

        if (TextUtils.isEmpty(MyApplication.myPreferences.readUid())) {


            LovelyToast.makeText(getContext(), "请登录之后操作", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);

            tender_award_sel_rb.setChecked(false);

        }


        getData();
        getTypeAccessToken();


        radiobuttonlinstener();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        borrowPop = new BorrowPop(getActivity());
    }

    private void initListener() {

        nolimit.setOnClickListener(this);
        tender_award_nosel_rb.setOnClickListener(this);
        daylimit.setOnClickListener(this);
        all_limit.setOnClickListener(this);
        directional_sel_rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    edit_pwd.setVisibility(View.VISIBLE);
                } else {
                    edit_pwd.setVisibility(View.GONE);
                }
            }
        });


    }

    public void radiobuttonlinstener() {


//投标奖励
        tender_award_sel_rb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                //reward_type_id = reward_type_list.get(0);
                if (isChecked) {
                    linearlayout_bili.setVisibility(View.VISIBLE);
                    flag_jiangli = true;

                } else {
                    linearlayout_bili.setVisibility(View.GONE);
                    check_toubiaobili.setChecked(false);
                    check_gudingmoney.setChecked(false);
                    toubiaojiangli.setText("");
                    editText.setText("");
                    no_yeid = "";
                    flag_jiangli = false;
                }


            }
        });


        if (TextUtils.isEmpty(MyApplication.myPreferences.readUid())) {

            LovelyToast.makeText(getContext(), "请登录之后进行操作", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else {

            check_toubiaobili.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        no_yeid = "1";

                        check_gudingmoney.setChecked(false);
                    } else {
                        no_yeid = "2";

                    }
                }
            });

            check_gudingmoney.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        no_yeid = "2";

                        check_toubiaobili.setChecked(false);
                    } else {
                        no_yeid = "1";


                    }
                }
            });


        }


    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View v) {
        if (TextUtils.isEmpty(MyApplication.myPreferences.readUid())) {


            LovelyToast.makeText(getContext(), "请登录之后进行操作", 0, LovelyToast
                    .SUCCESS, LovelyToast.SCALE);
        } else {
            switch (v.getId()) {
                case R.id.daylimit:


                    daishou_id = daishou_str.get(2);
                    img_third.setBackground(getResources().getDrawable(R.mipmap.choose_sel));
                    img_first.setBackground(getResources().getDrawable(R.mipmap.choose_no));
                    img_second.setBackground(getResources().getDrawable(R.mipmap.choose_no));
                    img_fourth.setBackground(getResources().getDrawable(R.mipmap.choose_no));
                    Toast.makeText(getContext(), daishou_id, Toast.LENGTH_SHORT).show();
                    break;
                case R.id.nolimit:
                    daishou_id = daishou_str.get(0);
                    img_first.setBackground(getResources().getDrawable(R.mipmap.choose_sel));
                    img_second.setBackground(getResources().getDrawable(R.mipmap.choose_no));
                    img_third.setBackground(getResources().getDrawable(R.mipmap.choose_no));
                    img_fourth.setBackground(getResources().getDrawable(R.mipmap.choose_no));
                    Toast.makeText(getContext(), daishou_id, Toast.LENGTH_SHORT).show();
                    break;


                case R.id.tender_award_nosel_rb:
                    daishou_id = daishou_str.get(1);
                    Toast.makeText(getContext(), daishou_id, Toast.LENGTH_SHORT).show();
                    img_second.setBackground(getResources().getDrawable(R.mipmap.choose_sel));
                    img_first.setBackground(getResources().getDrawable(R.mipmap.choose_no));
                    img_third.setBackground(getResources().getDrawable(R.mipmap.choose_no));
                    img_fourth.setBackground(getResources().getDrawable(R.mipmap.choose_no));
                    break;

                case R.id.all_limit:

                    daishou_id = daishou_str.get(3);
                    img_fourth.setBackground(getResources().getDrawable(R.mipmap.choose_sel));
                    img_first.setBackground(getResources().getDrawable(R.mipmap.choose_no));
                    img_third.setBackground(getResources().getDrawable(R.mipmap.choose_no));
                   img_first .setBackground(getResources().getDrawable(R.mipmap.choose_no));
                    Toast.makeText(getContext(), daishou_id, Toast.LENGTH_SHORT).show();
                    break;

                case R.id.et_01:


                    break;

                case R.id.iv_01:


                case R.id.et_05:

                    break;




            }
        }

    }

    private void getTypeAccessToken() {
        new JsonAccessToken("index/borrow", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        onTypeInfo(info.access_token);
                    } else {
                        //  UtilToast.show(getActivity(), "安全验证失败！");
                        LovelyToast.makeText(getContext(), "安全验证失败", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }

                }

            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                //  UtilToast.show(getActivity(), "安全验证失败！");
                LovelyToast.makeText(getContext(), "安全验证失败", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }
        }).execute(getActivity(), false);

    }


    private void onTypeInfo(String access_token) {


        new JsonBorrowindex(access_token, new AsyCallBack<JsonBorrowindex.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonBorrowindex.Info info) throws Exception {

                if (info != null) {
                    borrow_type_list = info.borrow_type;
                    // borrow_type_list.clear();
                    for (int i = 0; i < borrow_type_list.size(); i++) {
                        list_borrow_type.add(borrow_type_list.get(i).name);
                    }


                    borrow_use = info.borrow_use;
                    stringListBorrow_use.clear();
                    for (int i = 0; i < borrow_use.size(); i++) {
                        stringListBorrow_use.add(borrow_use.get(i).name);
                    }
//最高投资  private List<JsonBorrowindex.Info.ListContent> borrow_max = new ArrayList<>();
//                    private List<String> stringListBorrow_max = new ArrayList<>();

                    //最高投资
                    borrow_max = info.borrow_max;
                    stringListBorrow_max.clear();
                    for (int i = 0; i < borrow_max.size(); i++) {
                        stringListBorrow_max.add(borrow_max.get(i).name);
                    }
//最低投资
                    borrow_min = info.borrow_min;
                    borrow_min_list.clear();
                    for (int i = 0; i < borrow_min.size(); i++) {
                        borrow_min_list.add(borrow_min.get(i).name);
                    }

                    //有效时间
                    borrow_time = info.borrow_time;
                    borrow_time_list.clear();
                    for (int i = 0; i < borrow_time.size(); i++) {
                        borrow_time_list.add(borrow_time.get(i).name);
                    }

                    borrow_duration_month = info.borrow_duration_month;
                    myborrow_duration.clear();
                    for (int i = 0; i < borrow_duration_month.size(); i++) {
                        myborrow_duration.add(borrow_duration_month.get(i).name);
                    }


                    retypement = info.repayment_type;
                    //  str_retypement.clear();
                    for (int i = 0; i < retypement.size(); i++) {
                        str_retypement.add(retypement.get(i).name);
                    }


                    borrow_use_day = info.borrow_duration_day;
                    stringListBorrow_use_day.clear();
                    for (int i = 0; i < borrow_use_day.size(); i++) {
                        stringListBorrow_use_day.add(borrow_use_day.get(i).name);
                    }


//是否奖励
                    reward_type = info.reward_type;

                    for (int i = 0; i < reward_type.size(); i++) {
                        reward_type_list.add(reward_type.get(i).id);
                        reward_type_id = reward_type_list.get(i);
                    }
                    check_toubiaobili.setText(reward_type.get(0).name);
                    check_gudingmoney.setText(reward_type.get(1).name);


                    borrow_min = info.borrow_min;
                    borrow_min_list.clear();
                    for (int i = 0; i < borrow_min.size(); i++) {
                        borrow_min_list.add(borrow_min.get(i).name);
                    }


                    daishou = info.daishou;
                    //待收
                    for (int i = 0; i < daishou.size(); i++) {
                        daishou_str.add(daishou.get(i).id);
                    }


                    nolimit.setText(daishou.get(0).name);
                    tender_award_nosel_rb.setText(daishou.get(1).name);
                    daylimit.setText(daishou.get(2).name);
                    all_limit.setText(daishou.get(3).name);
                    //借款类型
                 /*   public List<JsonBorrowindex.Info.ListContent> borrow_type_list=new ArrayList<>();

                    List<String > list_borrow_type=new ArrayList<>();
                    private String borrow_type_id;*/


                }


            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                //   UtilToast.show(getActivity(), JsonBorrowindex.TOAST);
                LovelyToast.makeText(getContext(), JsonBorrowindex.TOAST, 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {

            }

        }).execute(getActivity(), false);

    }


    @Override
    public void onResume() {
        super.onResume();
        if (MyApplication.canClean) {

        } else {
            MyApplication.canClean = false;
        }

    }


}
